package com.liesheng.haylou.ui.reset;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.base.BaseFunActivity;
import com.liesheng.haylou.base.BaseVm;
import com.liesheng.haylou.bean.RegisterBean;
import com.liesheng.haylou.bean.ResetPswBean;
import com.liesheng.haylou.databinding.ActivityResetPswBinding;
import com.liesheng.haylou.net.HttpCallback;
import com.liesheng.haylou.net.HttpRequest;
import com.liesheng.haylou.ui.Login.LoginActivity;
import com.liesheng.haylou.ui.main.MainActivity;
import com.liesheng.haylou.utils.FormatUtil;
import com.liesheng.haylou.utils.ToastUtil;
import com.sahooz.library.Country;
import com.sahooz.library.PickActivity;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

/**
 * Created by Android Studio.
 * User: zhangzhiqiang
 * Date: 2019/12/18
 * Time: 11:41
 */
public class ResetPswActivity extends BaseActivity<ActivityResetPswBinding, BaseVm> {
    Intent intent;
    Country country;
    String password;
    String countryCode;
    String userPhone;
    String countryName;
    String smsCode;

    @Override
    protected ActivityResetPswBinding getmBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_reset_psw, null, false);
        //隐藏掉整个ActionBar
        return mBinding;
    }

    @Override
    protected BaseVm getViewModel() {
        return null;
    }

    @Override
    protected void initData() {
        showHeadLayout(false);
        mBinding.tvCountry.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.ivSpiner:
                intent = new Intent(this, PickActivity.class);
                startActivityForResult(intent, 111);
                break;
            case R.id.btnMSM:
                userPhone = mBinding.etPhone.getText().toString();
                password = mBinding.etRegisterPsd.getText().toString();
                smsCode = "FORGET";
                if (TextUtils.isEmpty(countryCode) && country != null) {
                    countryCode = "" + country.code;
                }

                if (TextUtils.isEmpty(userPhone)) {
                    ToastUtil.showToast("请输入正确的11位手机号！");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    ToastUtil.showToast("请输入密码！");
                    return;
                }
                if (TextUtils.isEmpty(smsCode)) {
                    ToastUtil.showToast("请输入验证码！");
                    return;
                }
                getSmS();
                break;
            case R.id.bt_regist:
                userPhone = mBinding.etPhone.getText().toString();
                password = mBinding.etRegisterPsd.getText().toString();
                smsCode = mBinding.etRegisterVerif.getText().toString();
                if (TextUtils.isEmpty(countryCode) && country != null) {
                    countryCode = "" + country.code;
                }
                if (TextUtils.isEmpty(countryName) && country != null) {
                    countryName = "" + country.name;
                }
                if (TextUtils.isEmpty(userPhone) && FormatUtil.validatePhone(userPhone)) {
                    ToastUtil.showToast("请输入正确的手机号码！");
                    return;
                }
                if (TextUtils.isEmpty(password) && FormatUtil.validateLoginPwd(password)) {
                    ToastUtil.showToast("请输入6-16位任意字符密码！");
                    return;
                }
                if (TextUtils.isEmpty(smsCode)) {
                    ToastUtil.showToast("请输入验证码！");
                    return;
                }
                register();
                break;
            case R.id.lltvNorLog:
                MainActivity.startBy(ResetPswActivity.this);
                break;
            case R.id.tvTiao:
                LoginActivity.startBy(ResetPswActivity.this);
                break;
        }
    }

    private void register() {
        Map<String, Object> map = new HashMap<>();
        map.put("countryCode", countryCode);
        map.put("countryName", countryName);
        map.put("userPhone", userPhone);
        map.put("password", password);
        map.put("smsCode", smsCode);
        requestHttp(buildHttpService().register(HttpRequest.getBody(map)), new HttpCallback<RegisterBean>() {
            @Override
            public void onNext(RegisterBean registerBean) {
                registerBean.getMsg();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestcode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestcode, resultCode, data);
        if (requestcode == 111 && resultCode == Activity.RESULT_OK) {
            country = Country.fromJson(data.getStringExtra("country"));
            if (country.flag != 0)
                mBinding.tvCountry.setText("+" + country.code);
        }
    }

    public static void startBy(BaseFunActivity activity) {
        Intent intent = new Intent(activity, ResetPswActivity.class);
        activity.startActivity(intent);
    }

    /**
     * 获取验证码
     */
    private void getSmS() {
        Map<String, Object> map = new HashMap<>();
        map.put("countryCode", countryCode);
        map.put("userPhone", userPhone);
        map.put("password", password);
        map.put("smsCode", smsCode);
        requestHttp(buildHttpService().getResetPsw(HttpRequest.getBody(map)), new HttpCallback<ResetPswBean>() {
            @Override
            public void onNext(ResetPswBean getInfoBean) {
                if (null != getInfoBean) {
                    getInfoBean.getMsg();
                }
            }
        });
    }
}
